(function ($) {
	$.uploadData = function () {
		return {
			init: function(){
				this.listener();
				this.initLayui();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
				$(".preUplad ul").on("click",".delete",function(){
					$(this).parents("li").remove();
				});
			},

			initLayui: function(){
				layui.use(['layer','form','element','upload'], function(){
	            	var $ = layui.jquery, layer = layui.layer, form = layui.form,element = layui.element,upload = layui.upload;
					//拖拽上传
					var uploadListIns=upload.render({
						elem: '#uploadDrag,#uploadButton'
						//,url: '/upload/'
						,auto: false
						,multiple: true
						//,size: 51200 //限制文件大小，单位 KB
						,accept: 'file'
						//,exts: 'mp3|mp4|amr|wav|cwep|ppt|word|png|jpg' //只允许文件后缀
						,choose: function(obj){
							var noUploadListHtml='';
							var uploadListHtml='';
							//读取本地文件
							obj.preview(function(index, file, result){
								//var indexArr=index.split("-");
								//var indexNo=indexArr[indexArr.length-1];

								//判断上传文件后缀及size限制
								var fileNameSuffixArr=file.name.split(".");
								var fileNameSuffix=fileNameSuffixArr[fileNameSuffixArr.length-1];
								var fileNameVertify=fileNameSuffix=="MP3"||fileNameSuffix=="MP4"||fileNameSuffix=="AMR"||fileNameSuffix=="WAV"||fileNameSuffix=="CWEP"||fileNameSuffix=="PPT"||fileNameSuffix=="PPTX"||fileNameSuffix=="DOC"||fileNameSuffix=="DOCX"||fileNameSuffix=="PNG"||fileNameSuffix=="JPG"||fileNameSuffix=="mp3"||fileNameSuffix=="mp4"||fileNameSuffix=="amr"||fileNameSuffix=="wav"||fileNameSuffix=="cwep"||fileNameSuffix=="ppt"||fileNameSuffix=="pptx"||fileNameSuffix=="doc"||fileNameSuffix=="docx"||fileNameSuffix=="png"||fileNameSuffix=="jpg";
								var fileSizeVertigy=file.size<10485760;
								if(fileNameVertify&&fileSizeVertigy){
									uploadListHtml='<li fileurl fileIndex='+index+' data-filename='+JSON.stringify(fileNameSuffixArr[0])+' data-filenamesuffix='+fileNameSuffix+' data-filesize='+file.size+'>'
										          //+'    <object class="uploadFileItem">'+file+'</object> '
												  + '    <span class="icon"></span>'+file.name+'<span class="fileSize">('+Math.floor(file.size/1024 * 10) / 10 +'K)</span>'
												  + '    <i class="layui-icon delete">&#x1007;</i>'
												  + '    <a class="fileContril">上传中...</a>'
												  + '    <div class="layui-inline-block">'
												  + '     	<input maxlength="25" type="text" name="remark"  placeholder="请输入资料名称（选填）" autocomplete="off" class="layui-input remark">'
												  + '     </div>'
												  + ' </li>';
									$(".preUplad ul").append(uploadListHtml);
									uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/", file, function (result) {
										var result=JSON.parse(result);
										if(result.status=="1000"){
											$(".preUplad ul").find("li[fileIndex="+index+"]").attr("fileurl",result.uploadFileDTO.fileId);
											$(".preUplad ul").find("li[fileIndex="+index+"] .fileContril").html("上传成功");
										}else{
											$(".preUplad ul").find("li[fileIndex="+index+"] .fileContril").html("上传失败");
										}
									});
								}else{
									$(".noUploadList").css("display","block");
									noUploadListHtml='<div>'+file.name+'</div>';
									$(".noUploadList").append(noUploadListHtml);
								}
							});
                            var file = $(".layui-upload-file")[0];
                            file.value = ''; //虽然file的value不能设为有字符的值，但是可以设置为空值
						}
					});

				});
			}

		}
	}();
    $.uploadData.init();
})(jQuery);